package cn.relx.org.controller;

import cn.relx.basic.config.BussinessException;
import cn.relx.basic.util.ExcelUtil;
import cn.relx.org.domain.ShopAuditLog;
import cn.relx.org.service.IShopService;
import cn.relx.org.domain.Shop;
import cn.relx.org.query.ShopQuery;
import cn.relx.basic.util.AjaxResult;
import cn.relx.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryByPage(query);
    }

    /**
     * 店铺入驻接口
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid Shop shop)
    {
        System.out.println(AjaxResult.me().success());
        shopService.settlement(shop);
        return AjaxResult.me().success();
    }
    @PostMapping("/settlement/again")
    public AjaxResult again(@RequestBody @Valid Shop shop)
    {
        shop.setState(0);
        shopService.update(shop);
        return AjaxResult.me().success();
    }
    /*
    批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        shopService.batchDel(ids);
        return AjaxResult.me().success();
    }
    /*
    导出
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xls",response);
    }
    /*
    导入
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.importExcel(shops);
    }

    @GetMapping("/echarts")
    public Map<String,Object> shopEcharsData(){
        return shopService.shopEcharsData();
    }

    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopAuditLog.setState(1L);
            shopService.pass(shopAuditLog);
            return AjaxResult.me().success();
        } catch (BussinessException e){
            e.printStackTrace();
            return AjaxResult.me().success(false).setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙,请稍后再试!");
        }
    }
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopAuditLog.setState(-1L);
            shopService.reject(shopAuditLog);
            return AjaxResult.me().success();
        } catch (BussinessException e){
            e.printStackTrace();
            return AjaxResult.me().success(false).setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙,请稍后再试!");
        }
    }
    @GetMapping("/audit/{id}")
    public AjaxResult audit(@PathVariable("id") Long id){
        shopService.updateByIdAudit(id);
        return AjaxResult.me().setMsg("激活成功").setSuccess(true);
    }
}
